package cn.cau.epidemicsurveysystem.control;

import cn.cau.epidemicsurveysystem.bean.*;
import cn.cau.epidemicsurveysystem.service.*;
import cn.cau.epidemicsurveysystem.vo.TemplateVO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.*;

import static java.lang.Integer.parseInt;

@RestController
public class WechatMissionController {
    @Autowired
    EpidMissionRecordService epidMissionRecordService;
    @Autowired
    StaffInfoService staffInfoService;
    @Autowired
    WechatTripService wechatTripService;
    @Autowired
    TemplateService templateService;
    @Autowired
    QuestionAnsService questionAnsService;
    //小程序-流调任务列表
    @RequestMapping(value = "/wechatUserMissionList",method = RequestMethod.GET)
    public List<Map<String, Object>> getWechatUserMissionList(@RequestParam("staffNo") String staffNo){
        StaffInfoBean staffInfoBean=staffInfoService.findStaffByNo(staffNo);
        if(staffInfoBean.getStaffPosition().equals("流调人员")){
            List<EpidMissionRecordBean> epidMissionRecordBeans=epidMissionRecordService.findEpidMissionRecordBeansByAssigneeId(staffInfoBean.getStaffId());
            List<Map<String, Object>> missionList= new ArrayList<Map<String, Object>>();
            for (EpidMissionRecordBean missionRecordBean : epidMissionRecordBeans) {
                if(missionRecordBean.getMissionState()==1)
                {
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("missionId",missionRecordBean.getMissionId());
                    map.put("missionName",missionRecordBean.getMissionName());
                    map.put("assignTime",missionRecordBean.getAssignTime());
                    missionList.add(map);
                }
            }
            return missionList;
        }
        else{
            List<EpidMissionRecordBean> epidMissionRecordBeans=epidMissionRecordService.findEpidMissionRecordBeansBySecondAssigneeId(staffInfoBean.getStaffId());
            List<Map<String, Object>> missionList= new ArrayList<Map<String, Object>>();
            for (EpidMissionRecordBean missionRecordBean : epidMissionRecordBeans) {
                if(missionRecordBean.getMissionState()==5)
                {
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("missionId",missionRecordBean.getMissionId());
                    map.put("missionName",missionRecordBean.getMissionName());
                    map.put("assignTime",missionRecordBean.getSecondPublishTime());
                    missionList.add(map);
                }
            }
            return missionList;
        }
    }
    //小程序-流调任务详情
    @RequestMapping(value = "/wechatMissionDetail",method = RequestMethod.GET)
    public EpidMissionRecordBean getWechatMissionDetail(@RequestParam("missionId") String missionId){
        return epidMissionRecordService.findEpidMissionRecordBeanByMissionId(parseInt(missionId));
    }
    //小程序-提交行程
    @RequestMapping(value = "/wechatTrip",method = RequestMethod.POST)
    public void submitWechatTrip(@RequestParam("missionId") String missionId, @RequestParam("tripRecords")String tripRecords){
        JSONArray tripRecordsJson = JSON.parseArray(tripRecords);
        EpidMissionRecordBean epidMissionRecordBean=epidMissionRecordService.findEpidMissionRecordBeanByMissionId(parseInt(missionId));
        if (tripRecordsJson.size() > 0) {
            for (int i = 0; i < tripRecordsJson.size(); i++) {
                PatientTripBean patientTripBean = new PatientTripBean();
                patientTripBean.setPatientId(epidMissionRecordBean.getPatientId());
                patientTripBean.setPatientName(epidMissionRecordBean.getPatientName());
                patientTripBean.setTripPlace(tripRecordsJson.getJSONObject(i).get("tripPlace").toString());
                patientTripBean.setArrivalTime(Timestamp.valueOf(tripRecordsJson.getJSONObject(i).get("arrivalDate").toString()+" "+tripRecordsJson.getJSONObject(i).get("arrivalTime").toString()+":00"));
                patientTripBean.setActivity(tripRecordsJson.getJSONObject(i).get("activity").toString());
                patientTripBean.setResidenceTime(tripRecordsJson.getJSONObject(i).get("residenceTime").toString());
                patientTripBean.setMissionId(Integer.valueOf(missionId));
                wechatTripService.savePatientTrip(patientTripBean);
            }
        }
    }
    //小程序-请求问卷
    @RequestMapping(value = "/wechatTemplate",method = RequestMethod.GET)
    public Map<String, Object> getWechatTemplate(@RequestParam("missionId") String missionId){
        EpidMissionRecordBean epidMissionRecordBean=epidMissionRecordService.findEpidMissionRecordBeanByMissionId(parseInt(missionId));
        Map<String, Object> map = new HashMap<String, Object>();
        TemplateVO templateVO =templateService.getTemplateVOById(epidMissionRecordBean.getEpidTemplateId());
        map.put("templateName",templateVO.getTemplate().getTemplateName());
        map.put("questions",templateVO.getQuestions());
        return map;
    }
    //小程序-提交答案
    @RequestMapping(value = "/wechatInvestigate",method = RequestMethod.POST)
    public void submitWechatInvestigate(@RequestParam("missionId") String missionId, @RequestParam("answers")String answers){
        JSONObject answersJson=JSON.parseObject(answers);
        for (Map.Entry<String, Object> answer : answersJson.entrySet()) {
            QuestionAnsBean questionAnsBean=new QuestionAnsBean();
            questionAnsBean.setQuestionId(parseInt(answer.getKey()));
            questionAnsBean.setAns(answer.getValue().toString().replace("[","").replace("\"","").replace("]",""));
            questionAnsBean.setPatientId(epidMissionRecordService.findEpidMissionRecordBeanByMissionId(parseInt(missionId)).getPatientId());
            questionAnsBean.setDate(new Timestamp(System.currentTimeMillis()));
            questionAnsBean.setMissionId(Integer.valueOf(missionId));
            questionAnsService.saveAns(questionAnsBean);
        }
        EpidMissionRecordBean epidMissionRecordBean=epidMissionRecordService.findEpidMissionRecordBeanByMissionId(parseInt(missionId));
        epidMissionRecordBean.setMissionState(2);
        epidMissionRecordBean.setMissionFinishedTime(new Date(System.currentTimeMillis()));
        epidMissionRecordService.save(epidMissionRecordBean);
    }
    //小程序-历史任务列表
    @RequestMapping(value = "/wechatUserHistoryList",method = RequestMethod.GET)
    public List<Map<String, Object>> getWechatUserHistoryList(@RequestParam("staffNo") String staffNo){
        StaffInfoBean staffInfoBean=staffInfoService.findStaffByNo(staffNo);
        List<Map<String, Object>> missionList= new ArrayList<Map<String, Object>>();
        if(staffInfoBean.getStaffPosition().equals("流调人员")){
            List<EpidMissionRecordBean> epidMissionRecordBeans=epidMissionRecordService.findEpidMissionRecordBeansByAssigneeId(staffInfoBean.getStaffId());
            for (EpidMissionRecordBean missionRecordBean : epidMissionRecordBeans) {
                if(missionRecordBean.getMissionState()!=0&&missionRecordBean.getMissionState()!=1&&missionRecordBean.getMissionState()!=5)
                {
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("missionId",missionRecordBean.getMissionId());
                    map.put("missionName",missionRecordBean.getMissionName());
                    map.put("assignTime",missionRecordBean.getAssignTime());
                    missionList.add(map);
                }
            }
        }
        else{
            List<EpidMissionRecordBean> epidMissionRecordBeans=epidMissionRecordService.findEpidMissionRecordBeansBySecondAssigneeId(staffInfoBean.getStaffId());
            for (EpidMissionRecordBean missionRecordBean : epidMissionRecordBeans) {
                if(missionRecordBean.getMissionState()!=5)
                {
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("missionId",missionRecordBean.getMissionId());
                    map.put("missionName",missionRecordBean.getMissionName());
                    map.put("assignTime",missionRecordBean.getSecondPublishTime());
                    missionList.add(map);
                }
            }
        }
        return missionList;
    }
    //小程序-二次确认
    @RequestMapping(value = "/wechatSecondMission",method = RequestMethod.POST)
    public void submitWechatSecondMission(@RequestParam("missionId") String missionId,@RequestParam("secondAnswer") String secondAnswer){
        EpidMissionRecordBean epidMissionRecordBean=epidMissionRecordService.findEpidMissionRecordBeanByMissionId(parseInt(missionId));
        epidMissionRecordBean.setSecondAnswer(secondAnswer);
        epidMissionRecordBean.setMissionState(6);
        epidMissionRecordBean.setSecondFinishedTime(new Date(System.currentTimeMillis()));
        epidMissionRecordService.save(epidMissionRecordBean);
    }
}
